package sn.gandal.agriculture.agrimobile.j2me.gui;


import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.BusinessServicesImpl;
import sn.gandal.agriculture.agrimobile.j2me.businessObjects.IBusinessServices;
import sn.gandal.agriculture.agrimobile.j2me.utilities.Configuration;

/**
 *
 * @author dysow
 */
public class MainMenu extends List implements CommandListener
{
    private static MIDlet midlet;
    private Command cmdSelect;
    private Command cmdExit;
    private GestionMembreMenu gestionAbonneMenu = null;
    private ConfigurationServeurForm configurationServeurForm = null;
 
    public MainMenu(MIDlet midlet)
    {
	super(Configuration.MENU_PRINCIPAL, List.IMPLICIT);

	this.midlet = midlet;
        
	this.initialiseComponents();
	this.initialiseCommands();
	this.addCommands();
	this.setCommandListener(this);
	this.setSelectCommand(this.cmdSelect);
    }

    private void initialiseComponents()
    {
	this.append(Configuration.MENU_GESTION_MEMBRES, null);
	this.append(Configuration.MENU_GESTION_PARCELLES, null);
	this.append(Configuration.MENU_GESTION_PRODUCTION, null);
	this.append(Configuration.MENU_GESTION_INTRANTS, null);
	this.append(Configuration.MENU_GESTION_ALERTES, null);
	this.append(Configuration.MENU_GESTION_MARCHES, null);
	this.append(Configuration.MENU_GESTION_COMMANDES, null);
    }

    private void initialiseCommands()
    {
	this.cmdSelect = new Command("Afficher", Command.ITEM, 1);
	this.cmdExit = new Command("Sortir", Command.EXIT, 2);
    }

    private void addCommands()
    {
	this.addCommand(this.cmdSelect);
	this.addCommand(this.cmdExit);
    }

    private void choiceGroupAction()
    {
	if (this.isGestionMembres())
	{
	 //   this.gestionAbonneAction();
	}
	else if (this.isGestionParcelles())
	{
	   // this.gestionPeseeAction();
	}
	else if (this.isGestionIntrants())
	{
	 //   this.transmissionAction();
	}
	else if (this.isGestionAlertes())
	{
	  //  this.identifiantAction();
	}
	else if (this.isGestionMarches())
	{
	 //   this.connexionAction();
	}
	else if (this.isGestionCommandes())
	{
	 //   this.webAddressAction();
	}
	
    }

    private boolean isGestionMembres()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_GESTION_MEMBRES);
    }

    private boolean isGestionParcelles()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_GESTION_PARCELLES);
    }

    private boolean isGestionProduction()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_GESTION_PRODUCTION);
    }

    private boolean isGestionIntrants()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_GESTION_INTRANTS);
    }

    private boolean isGestionAlertes()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_GESTION_ALERTES);
    }

    private boolean isGestionMarches()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_GESTION_MARCHES);
    }

    private boolean isGestionCommandes()
    {
	return this.getString(this.getSelectedIndex()).equals(Configuration.MENU_GESTION_COMMANDES);
    }

   
    private void configPaysAction()
    {
    }

    private void gestionAbonneAction()
    {
	this.getDisplay().setCurrent(this.getGestionAbonneMenu());
    }

  


//    private void transmissionAction()
//    {
//	this.getDisplay().setCurrent(this.getTransmissionDonneesForm());
//    }
//
//    private void identifiantAction()
//    {
//	this.getDisplay().setCurrent(this.getIdentificationForm());
//    }

    private void connexionAction()
    {
	try
	{
	//    ProgressForm progressForm = new ProgressForm("Tester Connexion",
//		    "L'application est en train de tester la connexion. Veuillez patienter...",
//		    this, this.getDisplay());

	    IBusinessServices iBusinessServices = BusinessServicesImpl.getInstance();
	  //  iBusinessServices.testerConnexion(progressForm, this.getDisplay());

	    // this.getDisplay().setCurrent(progressForm);
	    this.getDisplay().setCurrent(this);
	}
	catch (Exception ex)
	{
	    Alert alert = new Alert("Erreur Connexion",
		    "Une erreur s'est produite pendant la connexion avec le serveur. Veuillez re-essayez.",
		    null, AlertType.ERROR);

	    this.getDisplay().setCurrent(alert);

	}
    }

    private void webAddressAction()
    {
	this.getDisplay().setCurrent(this.getConfigurationServeurForm());
    }



    private void exitAction()
    {
	this.midlet.notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d)
    {
	if (c.equals(this.cmdSelect))
	{
	    this.choiceGroupAction();
	}
	else if (c.equals(this.cmdExit))
	{
	    this.exitAction();
	}
    }

//    private TransmissionDonneesForm getTransmissionDonneesForm()
//    {
//	if (this.transmissionDonneesForm == null)
//	{
//	    this.transmissionDonneesForm = new TransmissionDonneesForm(this.getDisplay(), this);
//	}
//
//	return this.transmissionDonneesForm;
//    }

//    private RechercheAbonneForm getRechercheAbonneForm()
//    {
//	System.out.println("MainMenu.getGestionPeseeMenu()");
//	if (this.rechercheAbonneForm == null)
//	{
//	    this.rechercheAbonneForm = new RechercheAbonneForm(this.getDisplay(), Configuration.CMD_GESTION_PESEE, this.getTitle());
//	}
//	return this.rechercheAbonneForm;
//    }

  

    private GestionMembreMenu getGestionAbonneMenu()
    {
	if (this.gestionAbonneMenu == null)
	{
	    this.gestionAbonneMenu = new GestionMembreMenu(this.getDisplay());
	}

	return this.gestionAbonneMenu;
    }

    private Display getDisplay()
    {
	return Display.getDisplay(this.midlet);
    }


    private ConfigurationServeurForm getConfigurationServeurForm()
    {
	if (this.configurationServeurForm == null)
	{
	    this.configurationServeurForm = new ConfigurationServeurForm(this.getDisplay(), this);
	}

	return this.configurationServeurForm;
    }


    public static MIDlet getMidlet()
    {
	return MainMenu.midlet;

    }
}
